﻿using System;
using System.Collections.Generic;

namespace Options.IBClient.WinApp.Extensions
{
   public static class IListOfDoubleExtensions
   {
      public static double Average(
         this IList<double> values, int startIndex, int endIndex)
      {
         values.MustBeNonNull();
         startIndex.MustBeGreaterThanOrEqualTo(0);
         startIndex.MustBeLessThan(values.Count);
         startIndex.MustBeLessThanOrEqualTo(endIndex);
         endIndex.MustBeGreaterThanOrEqualTo(0);
         endIndex.MustBeLessThan(values.Count);

         double sumFromStartIndexToEndIndex = 0;
         for (int i = startIndex; i <= endIndex; i++)
         {
            sumFromStartIndexToEndIndex += values[i];
         }
         int numberOfElementsInTheAverage = endIndex - startIndex + 1;
         double average = sumFromStartIndexToEndIndex / numberOfElementsInTheAverage;
         return average;
      }
   }
}
